'use strict';
angular.module('watchListCtrl', [ 'ui.bootstrap'])
app.controller('watchCtrl', ['$rootScope','i18nService','$scope','$http' ,'$state','$modal','$templateCache','$compile', 'Restangular', 'api_monitor', function ($rootScope,i18nService,$scope,$http,$state,$modal,$templateCache,$compile, Restangular, api_monitor) {
    $scope.langs=i18nService.getAllLangs();
    $scope.lang = 'zh-cn';
    var loginUser = $rootScope.user;
    console.log(' loginUser='+JSON.stringify( loginUser));
  //列表start
    
    $scope.apList = [];
    api_monitor.list().then(function(response){
        var myData = Restangular.stripRestangular(response);
        $scope.data=myData.list;
        var newList = [];
        for (var i = 0; i<$scope.data.length; i++) {
            var color = "";
            if($scope.data[i][3]="警告"){
                color = 'red';
            }
            newList.push({
                'id':   $scope.data[i][0],
                'time':$scope.data[i][1],
                'jing': $scope.data[i][2],
                'statu': $scope.data[i][3],
                'text':$scope.data[i][4],
                'lei': $scope.data[i][5],
                'ji': $scope.data[i][6],
                'from': $scope.data[i][7],
                'color':color
            })
            $scope.apList = newList;
            $scope.details = function (id,type) {
                var url = '';
                if(type == '北塔'){
                    url = 'assets/views/watch/tpl/beitawatch.html';

                }else if(type == '应用'){
                    url = 'assets/views/watch/tpl/detailwatch.html';
                }else if(type == '共济'){
                    url = 'assets/views/watch/tpl/gongjiwatch.html';

                }

                if(url != ''){
                    var modalInstance = $modal.open({
                        templateUrl: url ,
                        controller: function($scope, $modalInstance ){
                            var data
                            if(type == '北塔'){
                                api_monitor.beita(id).then(function(response){
                                    $scope.data = response.data;
                                    console.log($scope.data);
                                    $scope.alarmTime=moment($scope.data.alarmTime).format('YYYY-MM-DD HH:mm:ss');
                                    $scope.createTime=moment($scope.data.createTime).format('YYYY-MM-DD HH:mm:ss');
                                    // console.log("currentTime="+$scope.currentTime  + $scope.createTime);
                                });
                                $scope.cancel = function() {
                                    $modalInstance.dismiss('cancel');
                                };

                            }else if(type == '应用'){
                                api_monitor.app(id).then(function(response){
                                    $scope.data = response.data;
                                    console.log($scope.data);
                                    $scope.alarmTime=moment($scope.data.alarmTime).format('YYYY-MM-DD HH:mm:ss');
                                    $scope.createTime=moment($scope.data.createTime).format('YYYY-MM-DD HH:mm:ss');
                                    $scope.triggerTime=moment($scope.data.triggerTime).format('YYYY-MM-DD HH:mm:ss');
                                    $scope.alontime=$scope.data.age+"s" ;
                                    // $scope.
                                    console.log("$scope.data.alarmTime="+$scope.alontime);
                                });
                                $scope.cancel = function() {
                                    $modalInstance.dismiss('cancel');
                                };

                            }else if(type == '共济'){
                                api_monitor.gongji(id).then(function(response){
                                    $scope.data = response.data;
                                    console.log($scope.data);
                                    $scope.alarmTime=moment($scope.data.alarmTime).format('YYYY-MM-DD HH:mm:ss');
                                    $scope.createTime=moment($scope.data.createTime).format('YYYY-MM-DD HH:mm:ss');
                                    console.log($scope.data);
                                });
                                $scope.cancel = function() {
                                    $modalInstance.dismiss('cancel');
                                };

                            }
                            //创建事件start
                            $scope.selectRowFunction = function(data){
                                console.log('data='+JSON.stringify(data));
                                var datamodel= {model:{'incident':{'title':type+'故障','source':{'id':3,'code':'monitor','source':'监控系统'},'description':data.description,'alarmId':data.alarmId},'alarmType':{'id':data.alarmId,'type':'告警'}},cancelUrl:'app.watch.list'};
                                //var datamodel= {'incident':{'title':type+'故障','source':{'id':3,'code':'monitor','source':'监控系统'},'description':data.description}};
                                $state.go('app.incident.watch',{'model':JSON.stringify(datamodel)});

                                $modalInstance.dismiss('cancel');
                            };
                            //创建事件end
                            // 'title':'type','requester':'loginUser','source':'data.source','description':'data.description'}},'type':{'id':告警id,'type':'告警'}
                        },
                        size:'lg',
                        resolve: {
                            items: function() {
                                return newList;
                            },
                        }
                    });
                }
            };   
        }
    });
   //列表end
   //柱形图start
    $scope.apcpu = [];
    api_monitor.cpuList().then(function(response){
        var myData = Restangular.stripRestangular(response);
        $scope.data=myData.list;
        var newnum = [];
        for (var i = 0; i <$scope.data.length; i++) {
            var load=$scope.data[i][1];
            // console.log($scope.load);
            var color = "";
            if (load < 60) {
                color = 'rgba(24, 220, 24, 1)';
            } else if (load >= 60 && load < 80) {
                color = 'yellow';
            } else if (load >= 80) {
                color = 'red';
            }
            // console.log(color); 
            newnum.push({
                'id':   $scope.data[i][0],
                'per':$scope.data[i][1],
                'color': color

            })
            $scope.apcpu = newnum;
        }
      
        // console.log($scope.apnum);           
    })  

    $scope.apnei = [];
    api_monitor.memList().then(function(response){
        var myData = Restangular.stripRestangular(response);
        $scope.data=myData.list;
        var newnei = [];
        for (var i = 0; i <$scope.data.length; i++) {
            var load=$scope.data[i][1];
            // console.log($scope.load);
            var color = "";
            if (load < 60) {
                color = 'rgba(24, 220, 24, 1)';
            } else if (load >= 60 && load < 80) {
                color = 'yellow';
            } else if (load >= 80) {
                color = 'red';
            }
            newnei.push({
                'id':   $scope.data[i][0],
                'per':$scope.data[i][1],
                'color': color

            })
            $scope.apnei = newnei;
        }
       
        // console.log($scope.apnei);           
    })  
    $scope.apchu = [];
   api_monitor.storageList().then(function(response){
        var myData = Restangular.stripRestangular(response);
         $scope.data=myData.list;
        var newchu = [];
        for (var i = 0; i <$scope.data.length; i++) {
            var load=$scope.data[i][1];
            // console.log($scope.load);
            var color = "";
            if (load < 60) {
                color = 'rgba(24, 220, 24, 1)';
            } else if (load >= 60 && load < 80) {
                color = 'yellow';
            } else if (load >= 80) {
                color = 'red';
            }
            // console.log(color); 
            newchu.push({
                'id':   $scope.data[i][0],
                'per':$scope.data[i][1],
                'color': color

            })
            $scope.apchu = newchu;
        }
        // console.log(newchu); 

        // console.log($scope.apchu);           
    })  
     //柱形图end
     //百分图start
   api_monitor.transData().then(function(response){
        var myData = Restangular.stripRestangular(response);
        $scope.data=myData.list;
        if ($scope.data && $scope.data.length == 1) {
            var dataArr = $scope.data[0];//交易信息数据：依次为交易量(transCount)，交易成功量(succCount)，交易失败量(failCount)，响应时间(respTime)，响应率(succRate)
            // console.log(dataArr);
            var transCount = (dataArr[0] == null ? 0 : dataArr[0]), succCount = (dataArr[1] == null ? 0 : dataArr[1]), 
            failCount = (dataArr[2] == null ? 0 : dataArr[2]), respTime = (dataArr[3] == null ? 0 : dataArr[3]), 
            succRate = (dataArr[4] == null ? 0 : dataArr[4]);
            // changeTime(respTime);//设置响应时间
            // changePercent(succRate);//设置响应率
            $("#transCount").empty();
            $("#transCount").html(transCount);//设置交易量提示信息
            $("#succCount").empty();
            $("#succCount").html(succCount);//设置交易量提示信息
            $("#failCount").empty();
            $("#failCount").html(failCount);
            var newjiao = [];
            for (var i = 0; i <$scope.data.length; i++) {
            var time=$scope.data[i][3]*1000;
                time=parseFloat(time).toFixed(0);
                var lv=$scope.data[i][4]*100;
                lv=parseFloat(lv).toFixed(2);
                // console.log(time);
            console.log(lv);

            newjiao.push({
                'all':   $scope.data[i][0],
                'success':$scope.data[i][1],
                'fail':$scope.data[i][2],
                'time':time,
                'lv':lv
        })
         $scope.apjiao = newjiao;
     }


       $('#tradeNum').highcharts({
        chart: {
            type: 'pie',
            options3d: {
                enabled: true,
                alpha: 45,
                beta: 0
            },
            width: 125,
            height: 100
        },
        exporting: {
            enabled: false
        },
        title: {
            text: ''
        },
        subtitle: {                                                        
            text: ''                                  
        },
        tooltip: {
            pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
        },
        plotOptions: {
            pie: {
                allowPointSelect: true,
                cursor: 'pointer',
                depth: 35,
                    size: '100%',//圆饼相对容器的直径
                    dataLabels: {
                        enabled: true,
                        format: '{point.percentage:.1f} %',  
                        distance:-2
                    }
                }
            },
            credits: {                                                         
                enabled: false                                               
            },
            series: [{
                type: 'pie',
                name: '占用百分比',
                data: [
                {
                    name: '失败交易数',
                    y: failCount,
                    color:'#59A4D8'
                },
                {
                    name: '成功交易数',
                    y: succCount,
                    sliced: true,
                    selected: true,
                    color:'#18DC18'
                }
                ]
            }]
        });    
}
});

//百分图end


$scope.aphover={};
$scope.loadhover =  function(id,shortname,x,y) {
    api_monitor.bankTip(id).then(function(response){
        var myData = Restangular.stripRestangular(response);
        var newhover = {};
           // console.log("y="+y);
           newhover={
            'shortname':shortname,
            'all':myData.total,
            'normal': myData.normalTotal,
            'worn': myData.warnTotal,
        }
        $scope.aphover = newhover;
        $scope.newDiv.innerHTML =  
        "<div>"+$scope.aphover.shortname +
        "总台数"+$scope.aphover.all+
        "正常"+$scope.aphover.normal +
        "警告"+$scope.aphover.worn +
        '</div>';
    });

 

    // console.log($scope.aphover); 
    $scope.newDiv = document.createElement("div");
    $scope.newDiv.id = id;
    $scope.newDiv.style.position = "absolute";
    $scope.newDiv.style.zIndex = "3";
    var newDivWidth = 180;
    var newDivHeight = 80;
    $scope.newDiv.style.width = newDivWidth + "px";
    $scope.newDiv.style.height = newDivHeight + "px";
    $scope.newDiv.style.top = y-10+"px";
    $scope.newDiv.style.left = x+280 + "px";
    $scope.newDiv.style.background = "#EFEFEF";
    $scope.newDiv.style.border = "1px solid #860001";
    $scope.newDiv.style.padding = "5px";

    document.body.appendChild( $scope.newDiv);
}   
$scope.loadout =  function() { 
  document.body.removeChild($scope.newDiv);
}    

$scope.apClient=[];
$scope.loadClient = function(id,size,shortname,x,y) {
    // $http({     
    //     url: "http://127.0.0.1:9004/services/ftpFile/monitor/devList/"+ id,
    //     method: "GET"
    // }).success(function(data, status, header, config){
api_monitor.devList(id).then(function(response){
        var myData = Restangular.stripRestangular(response);
     $scope.data=myData.list;
     // console.log("$scope.data.length="+$scope.data.length);


     var newClient =[];
     for (var i = 0; i <$scope.data.length; i++) {

        newClient.push({
            'id':$scope.data[i][0],
            'name':$scope.data[i][1],
            'statu': $scope.data[i][2],
            'shortname': shortname
        })
    }

    $scope.apClient = newClient;
    var modalInstance = $modal.open({
        templateUrl: 'assets/views/watch/tpl/coordwatch.html',
                                controller: 'ModalInstanceCtrl',//'CustomformCtrl',//
                                size: size,
                                resolve: {
                                    items: function() {
                                        return newClient;
                                    },
                                    
                                }
                            });


// console.log('$scope.apClient='+JSON.stringify($scope.apClient));
});


}
function setcolor(id,shortname) {
  api_monitor.bankTip(id).then(function(response){
        var myData = Restangular.stripRestangular(response);
   var color = "";
   if (myData.warnTotal==0) {
    color = 'green';
}else if (myData.warnTotal!==0) {
    color = 'red';
}
for ( var aptu_id in $scope.aptu)
{
        // console.log("loadhover() item.id="+$scope.aptu[aptu_id].id)
        if($scope.aptu[aptu_id].id==id)
        {
            // console.log("item.id="+id+",item.shortname="+$scope.aptu[aptu_id].shortname)
            $scope.aptu[aptu_id].color=color
        }
    }
});

}



api_monitor.banklist().then(function(response){
        var myData = Restangular.stripRestangular(response);
         $scope.data=myData.list;
    var newtu = [];
    for (var i = 0; i <$scope.data.length; i++) {
        newtu.push({
            'shortname':   $scope.data[i][0],
            'x':$scope.data[i][1],
            'y': $scope.data[i][2],
            'id': $scope.data[i][3],
            'color': 'green',

        })
        setcolor($scope.data[i][3],$scope.data[i][0])
        $scope.aptu = newtu;
    }   

    console.log('$scope.aptu='+JSON.stringify($scope.aptu));
})  


}]);
